package bidsanity

import static org.junit.Assert.*
import org.junit.*
import grails.converters.JSON
import com.icegreen.greenmail.util.GreenMailUtil

class ListingCompleteServiceIntegrationTests {

    def jmsService
    def greenMail


    @Before
    void setUp() {
        // Setup logic here
    }

    @After
    void tearDown() {
        // Tear down logic here
    }

    @Test
    void testEmailOnCompleteNoBid() {

        def seller = "test@example.com"
        def description = "test description"
        def details = [:]
        details.listingDescription = description
        details.sellerEmail = seller
        details.listingId = 1

        jmsService.send(queue: 'listing.nobids', (details as JSON).toString());
        
        Thread.sleep(3000)
        
        assertEquals(1, greenMail.getReceivedMessages().length)

        def mail = greenMail.getReceivedMessages()[0]
        greenMail.deleteAllMessages()
        assertEquals("listings@bidsanity.com", GreenMailUtil.getAddressList(mail.from))
        assertEquals(seller, mail.to)
        assertEquals("Listing complete with no bids for: $description".toString(), mail.subject.toString())

    }

    @Test
    void testEmailOnCompleteListing() {

        def seller = "test@example.com"
        def description = "test description"
        def winner = "other@nowhere.net"
        def details = [:]
        details.listingDescription = description
        details.sellerEmail = seller
        details.listingId = 1
        details.winnerEmail = winner
        details.winningAmount = 200

        jmsService.send(queue: 'listing.complete', (details as JSON).toString());

        Thread.sleep(3000)

        assertEquals(2, greenMail.getReceivedMessages().length)

        def mail = greenMail.getReceivedMessages()
        greenMail.deleteAllMessages()

        assertEquals("listings@bidsanity.com", GreenMailUtil.getAddressList(mail[0].from))
        assertEquals(seller, mail[0].to)
        assertEquals("Listing complete for: $description".toString(), mail[0].subject.toString())
    }
}
